package com.ruanko.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.ruanko.dao.DownloadFileDao;
import com.ruanko.dao.UploadFileDao;
import com.ruanko.dao.impl.DownloadFileDaoImpl;
import com.ruanko.dao.impl.UploadFileDaoImpl;
import com.ruanko.entity.DownloadFileBean;
import com.ruanko.entity.UploadFileBean;
import com.ruanko.service.FileService;

public class FileServiceImpl implements FileService {
	private UploadFileDao uploadFileDao;
	private DownloadFileDao downloadFileDao;
	private String userid;
	
	public FileServiceImpl() {
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		this.uploadFileDao = new UploadFileDaoImpl();
		this.downloadFileDao = new DownloadFileDaoImpl();
		this.userid = (session.getAttribute("userid")).toString();
	}

	@SuppressWarnings("unchecked")
	/*
	 * @file:文件域列表
	 * @fileName:上传文件的文件名
	 * @path:文件上传的目录，相对目录。 说明：文件上传所存放目录的规则：网站根目录下 files/会员ID/网站类型名称/上传文件分类名称/
	 * @返回结果：一个存放上传文件所在位置相对路径的ArrayList
	 */
	public ArrayList upload(List file, List fileName, String path)
			throws IOException {
		
		HttpSession session = ServletActionContext.getRequest().getSession(true);
		String userid = session.getAttribute("userid").toString();
		path += userid+"/";
		// String root = ServletActionContext.getRequest().getRealPath(path);
		String root = ServletActionContext.getServletContext()
				.getRealPath(path);
		File dir = new File(root);
		if (dir.exists() == false) {
			dir.mkdirs();
		}
		ArrayList list = new ArrayList();
		ArrayList<UploadFileBean> uploadFileList = new ArrayList<UploadFileBean>();
		for (int i = 0; i < file.size(); ++i) {
			
			UploadFileBean uploadFileBean = new UploadFileBean();
			InputStream is = new FileInputStream(file.get(i).toString());
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 格式化时间输出
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 格式化时间输出
//			String Rname = sdf.format(new Date());// 取得当前时间，Date()是java.util包里的，这作为真实名称
			String s = UUID.randomUUID().toString();
			
			String name = fileName.get(i).toString();// 得到上传文件的原名称
			uploadFileBean.setbFilename(name);
//			name = Rname + "-" + name;// 重命名文件名称,命名规则为：时间+原文件名称
			
			String[] string = name.split("\\.");
			name = s+"."+string[string.length-1]; //重命名文件名称
			uploadFileBean.setaFilename(name);
			uploadFileBean.setUserID(userid);
			uploadFileBean.setUploadTime(sdf2.format(new Date()));
			File destFile = new File(root, name);

			OutputStream os = new FileOutputStream(destFile);

			byte[] buffer = new byte[1024];

			int length = 0;

			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			list.add(path + name);
			is.close();
			os.close();
			uploadFileList.add(uploadFileBean);
		}
		uploadFileDao.insert(uploadFileList);
		return list;
	}
	/**
	 * 查询用户上传的文件记录（上传时间+文件名）
	 * @return
	 */
	public List<UploadFileBean> getUserUploadFileRecord(){
		return uploadFileDao.getUserUploadFileRecord(userid);
	}
	/**
	 * 查询用户下载的文件记录（下载时间+文件名）
	 * @return
	 */
	public List<DownloadFileBean> getUserDownloadFileRecord() {
		return downloadFileDao.getUserDownloadFileRecord(userid);
	}
	/**
	 * 插入下载文件记录
	 * @param filenameArray
	 * @return
	 */
	public boolean insertDownloadRecord(String[] filenameArray) {
		boolean b = false;
		ArrayList<DownloadFileBean> downloadFileList = new ArrayList<DownloadFileBean>();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 格式化时间输出
		String downtime = sdf2.format(new Date());
		for (int i = 0; i < filenameArray.length; i++) {
			DownloadFileBean downloadFileBean = new DownloadFileBean();
			downloadFileBean.setDownloadTime(downtime);
			downloadFileBean.setaFilename(filenameArray[i]);
			downloadFileBean.setUserid(userid);
			downloadFileList.add(downloadFileBean);
		}
		b = downloadFileDao.insert(downloadFileList);
		return b;
	}
	
}
